#ifndef __UTILS__LOAD_SIDE__
#define __UTILS__LOAD_SIDE__

#include "../include/Grid.hpp"
#include <fstream>
#include <map>
#include <string>
#include <functional>

// input: size_t = hash<unsigned long>(idx1) + hash<unsigned long>(idx2)
//
// output: whether {exist, id ,marker}
// exist: 0 no, 1 yes
// id: when exist = 1, the index of middle point
// marker: 0 inside region, 1 on boundary 

inline size_t trans_index_pair(unsigned long c, unsigned long d)
{
    std::hash<std::string> f;
    if(c > d)
    {
        std::swap(c,d);
    }
    std::string str1 = std::to_string(c);
    std::string str2 = std::to_string(d);
    size_t key = f(str1) + f(str2);
//    std::cout << "key="  << key << std::endl;
    return key;
}


std::map<size_t, std::array<size_t, 3> > load_side()
{
    std::map<size_t, std::array<size_t, 3> > index_side;
    std::ifstream fin("../data/domain.s");
    unsigned long num_side;
    fin >> num_side;
    std::cerr << "reading domain.s which has " << num_side << " sides in total" << std::endl;
    char sn[100];
    unsigned long c, d;
    long ea, eb;
    bool marker;
    for(unsigned long idx=0; idx<num_side; idx++)
    {
        fin >> sn \
            >> c >> d \
            >> ea >> eb \
            >> marker;
        size_t key = trans_index_pair(c, d);
        std::array<size_t, 3> value = {0,0,0};
        if(marker == 1)
        {
            value[2] = 0;
        }
        else
        {
            value[2] = 1;
        }
        index_side[key] = value;
    }
    fin.close();
    return index_side;
}

#endif

/***test function***
int main()
{
    auto p = load_side();
    auto key1 = trans_index_pair(2,6);
    auto key2 = trans_index_pair(3,5);
    std::cout <<"key:"<< key1 << " value:"<< p[key1] << std::endl;
    std::cout <<"key:"<< key2 << " value:"<< p[key2] << std::endl;
    return 0;
}
****end test******/
